﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAOMall;

namespace DTOMall
{
    public class ShopsDTO : DBTool, ICommon
    {
        public bool insert(object obj)
        {
            if (obj == typeof(Shops))
            {
                try
                {
                    using (SqlConnection con = GetConnection())
                    {
                        Shops sh = (Shops)obj;
                        using (SqlCommand cmd = con.CreateCommand())
                        {
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandText = "Insert_Shops";
                            cmd.Parameters.AddWithValue("@name", sh.name);
                            cmd.Parameters.AddWithValue("@namePlain", sh.name_plain);
                            cmd.Parameters.AddWithValue("@description", sh.description);
                            cmd.Parameters.AddWithValue("@content", sh.content);
                            cmd.Parameters.AddWithValue("@createdDate", sh.created_date);
                            cmd.Parameters.AddWithValue("@status", sh.status);
                            object o = cmd.ExecuteScalar();
                            CloseConnection();
                            if (o == typeof(int))
                            {
                                return ((int)o > 0) ? true : false;
                            }
                            else
                            {
                                return false;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }

            }
            else
            {
                return false;
            }
        }

        public bool update(object obj)
        {
            if (obj == typeof(Users))
            {
                try
                {
                    using (SqlConnection con = GetConnection())
                    {
                        Shops sh = (Shops)obj;
                        using (SqlCommand cmd = con.CreateCommand())
                        {
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandText = "Update_Shops";
                            cmd.Parameters.AddWithValue("@id", sh.id);
                            cmd.Parameters.AddWithValue("@name", sh.name);
                            cmd.Parameters.AddWithValue("@namePlain", sh.name_plain);
                            cmd.Parameters.AddWithValue("@description", sh.description);
                            cmd.Parameters.AddWithValue("@content", sh.content);
                            cmd.Parameters.AddWithValue("@status", sh.status);
                            object o = cmd.ExecuteScalar();
                            CloseConnection();
                            if (o == typeof(int))
                            {
                                return ((int)o > 0) ? true : false;
                            }
                            else
                            {
                                return false;
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }
            }
            else
            {
                return false;
            }
        }

        public bool delete(int id)
        {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "Delete_Shops";
                        cmd.Parameters.AddWithValue("@id", id);
                        object o = cmd.ExecuteScalar();
                        if (o == typeof(int))
                        {
                            return ((int)o > 0) ? true : false;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public System.Data.DataTable getAll()
        {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {

                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "View_All_Shops";
                        DataTable dt = GetTable(cmd);
                        return dt;
                    }

                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public object getByID(int id)
        {
            try
            {
                using (SqlConnection con = GetConnection())
                {
                    using (SqlCommand cmd = con.CreateCommand())
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "View_Shops_By_ID";
                        cmd.Parameters.AddWithValue("@id", id);
                        DataTable dt = GetTable(cmd);
                        Shops sh = new Shops();
                        sh.id = id;
                        sh.name = dt.Rows[0]["address"].ToString();
                        sh.name_plain = dt.Rows[0]["name_plain"].ToString();
                        sh.status = Int32.Parse(dt.Rows[0]["status"].ToString());
                        sh.content = dt.Rows[0]["content"].ToString();
                        sh.created_date = DateTimeLib.Convert2Date(dt.Rows[0]["created_date"].ToString());
                        sh.description = dt.Rows[0]["description"].ToString();
                        return sh;
                    }

                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
    }
}
